/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.ball.views.maps
{
	import com.puny.ball.utils.handlers.DefaultWorldScrollHandler;
	import com.puny.ball.views.obstacles.MovingBoxObstacle;
	import com.puny.ball.views.obstacles.MovingCircleObstacle;
	import com.puny.ball.views.obstacles.Obstacle;
	import com.puny.mvc.views.View;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class MapView extends View
	{
		protected static var _debugFlag:Boolean = true;
		private const TWO_PI:Number = 2 * Math.PI;
		private const RADIAN:Number = Math.PI / 180;
//		private const TEN_DGREE:Number = Math.PI / 18;
		private const _drop:Point = new Point();
		private const _direction:Point = new Point();
		private var _hole:MovieClip;
		private var _traps:Array = new Array();
		private var _drops:Array = new Array();
		private var _obstacles:Array = new Array();
		private var _accelerators:Array = new Array();
		private var _frictions:Array = new Array();
		private var _asset:String;
		private var _topLayers:Array = new Array();
		private const _scrollHandlers:Array = new Array();
		
		public function MapView(src:Object,name:String)
		{
			_asset = name;
			super(src);
		}

		protected function setupScrollHandler():void {
			_scrollHandlers.push(new DefaultWorldScrollHandler());
		}
		
		public function get scrollHandler():Array {
			return _scrollHandlers;
		}
		
		public static function set debugFlag(value:Boolean):void {
			_debugFlag = value;
		}
		
		public function get traps():Array {
			return _traps;
		}

		public function get obstacles():Array {
			return _obstacles;
		}
		
		public function get accelerators():Array {
			return _accelerators;
		}
		
		public function get frictions():Array {
			return _frictions;
		}
		
		public function get topLayers():Array {
			return _topLayers;
		}
//		public function get drops():Array {
//			return _drops;
//		}
		
		protected function get hole():MovieClip {
			return _hole;
		}
		
		protected function pickUp(child:DisplayObject):void {}
		
		override protected function setup():void {
			_hole = getMovieClip(_asset, true, false);
			var i:int;
			var child:DisplayObject;
			var rect:Rectangle;
			var matrix:Matrix;
			for(i =0;i < _hole.numChildren;i++) {
				child = _hole.getChildAt(i);
				if (child is Obstacle) {
					_obstacles.push(child);
				} else if (child is Accelerator) {
					child.visible = _debugFlag;
					_accelerators.push(child);
				} else if (child is Friction) {
					_frictions.push(child);
				} else if (child is Trap) {
					_traps.push(child);
				} else if (child is Drop) {
					child.visible = _debugFlag;
					_drops.push(child);
				}
				pickUp(child);
			}
		}
		public function drop(px:Number, py:Number):Point {
			var r:Number = Infinity;
			var l:Number;
			for each(var pt:Drop in _drops) {
				l = pt.distance(px,py);
				if (l < r) {
					r = l;
					_drop.x = pt.x;
					_drop.y = pt.y;
					_direction.x = 5 * Math.cos(pt.rotation * RADIAN);
					_direction.y = 5 * Math.sin(pt.rotation * RADIAN);
				}
			}
			return _drop;
		}
		
		protected function get drops():Array {
			return _drops;
		}
		
		protected function get dropped():Point {
			return _drop;
		}
		
		public function droppedDirection():Point {
			return _direction;
		}
		public function debug():String {
			var i:int;
			var child:DisplayObject;
			var string:String = _asset + "\r\n";
			for(i =0;i < _obstacles.length;i++) {
				child = _obstacles[i];
				string += "\t\t\tctrl.rotate(";
				string += i;
				string += ",";
				string += child.rotation;
				string += ");\r\n";	
				if (child is MovingBoxObstacle ) {
					string += (child as MovingBoxObstacle).debugInfo();
				} else if (child is MovingCircleObstacle) {
					string += (child as MovingCircleObstacle).debugInfo();
				}				
			}
			return string;
		}		
	}
}